class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> hash;
        for(const auto& e : nums) hash.insert(e);
        int ans = 0, size = nums.size();
        for(const auto& e : hash)
        {
            if(hash.count(e - 1)) continue;
            int cur = e; while(hash.count(cur)) ++cur;
            ans = max(cur - e, ans);
            if(ans > size / 2) break;
        }
        return ans;
    }
};